/*
 *  Rather than stick all the score stuff in the BoardView, I decided
 *  it would look cleaner to just stick the Score elements in a different panel.
 */
package com.samskievert.cactusboom;

import com.threerings.media.VirtualMediaPanel;
import com.threerings.toybox.util.ToyBoxContext;

import com.threerings.crowd.client.PlaceView;
import com.threerings.crowd.data.PlaceObject;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Rectangle;

 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
 import com.threerings.presents.dobj.DSet;
 
 import com.threerings.presents.dobj.ElementUpdatedEvent;
 import com.threerings.presents.dobj.ElementUpdateListener;
 
 import com.threerings.presents.dobj.EntryAddedEvent;
 import com.threerings.presents.dobj.EntryUpdatedEvent;
 import com.threerings.presents.dobj.EntryRemovedEvent;
 import com.threerings.presents.dobj.SetListener;

 import java.awt.event.MouseEvent;
 import java.awt.event.MouseMotionAdapter;
 import java.awt.event.MouseAdapter;
 
 import java.awt.Font;
 import com.samskivert.swing.Label;
 import com.threerings.media.animation.FloatingTextAnimation;
 import com.threerings.parlor.media.ScoreAnimation;
 import com.threerings.media.sprite.Sprite;
import javax.swing.plaf.SliderUI;

import com.threerings.util.Name;
import com.samskivert.swing.GroupLayout;
import javax.swing.JButton;


import com.threerings.media.image.BufferedMirage;
import com.threerings.media.image.Mirage;


public class CBScoreView extends VirtualMediaPanel
    implements PlaceView, SetListener, ElementUpdateListener
{

    CowboySprite[] cSprites = new CowboySprite[4];
    DataSprite[] dSprites = new DataSprite[4];
    
    
     /**
     * Constructs a view which will initialize itself and prepare to display
     * the game board.
     */
    public CBScoreView (ToyBoxContext ctx, CBController ctrl, CBTerritoryView tview)
    {
        super(ctx.getFrameManager());
        _ctx = ctx;
        _ctrl = ctrl; 
        _tview = tview;
    }
    
    public void addSprites()
    {
        //first clean out any sprites we've already got
        clearSprites();
        
        Name[] names = _gameobj.getPlayers();
        //System.out.println("I found " + names.length + " players.");
        CowboySprite[] cSprites = new CowboySprite[names.length];
        DataSprite[] dSprites = new DataSprite[names.length];
        
        int[] locX = {0,0,110,110};
        int[] locY = {0,70,0,70};
        
        for(int i = 0; i < names.length; i++){
            CBObject.Cowboy a = new CBObject.Cowboy();
            a.pieceId = i;
            a.owner = i;
            a.x = 0; a.y = 0;
            CowboySprite csa = new CowboySprite(a,new Dimension(8,8),_tview.getTVImage(i));
            csa.scale = 1.0;
            addSprite(csa);
            csa.setLocation(locX[i] + 2,locY[i] + 2);
            
            cSprites[i] = csa;
            csa.invalidate();
            
            DataSprite ds = new DataSprite(_gameobj, i);
            addSprite(ds);
            ds.setLocation(locX[i],locY[i]);
            
            dSprites[i] = ds;
            ds.invalidate();
        }
    }
    
    
    // from interface PlaceView
    public void willEnterPlace (PlaceObject plobj)
    {
        _gameobj = (CBObject)plobj;
        _gameobj.addListener(this);
        
        addSprites();
        
        //need to set the scores here
    }

    // from interface PlaceView
    public void didLeavePlace (PlaceObject plobj)
    {
         _gameobj.removeListener(this);
         _gameobj = null;
    }

     // from interface SetListener
     public void entryAdded (EntryAddedEvent event)
     {
         
     }
     
     //from interface SetListener
     public void entryUpdated(EntryUpdatedEvent event)
     {
         
     }
 
     // from interface SetListener
     public void elementUpdated (ElementUpdatedEvent event)
     {
         if (event.getName().equals(CBObject.TURN_POINTS) || event.getName().equals(CBObject.EXPLOSIONS)
                                                          || event.getName().equals(CBObject.LIVES_LEFT)
                                                          || event.getName().equals(CBObject.PLAYER_EXPERIENCE)
                                                          || event.getName().equals(CBObject.PLAYER_LEVEL)
                                                          || event.getName().equals(CBObject.MOVE_POINTS)   ) {
             //System.out.println("I was told to change the " + event.getIndex() + "th value from " + event.getOldValue() + " to " + event.getValue());
             if(dSprites[event.getIndex()] != null){
                 dSprites[event.getIndex()].invalidate();
             }else{
                //System.out.println(event.getIndex() + " is null.");
                addSprites();
             }
             // update the sprite that is displaying the updated piece
             //CBObject.Cactus cactus = (CBObject.Cactus)event.getEntry();
             //_sprites.get(cactus.getKey()).updateCactus(cactus);
         }
         
     }
 
     // from interface SetListener
     public void entryRemoved (EntryRemovedEvent event)
     {
         
     }
     
   
   @Override // from JComponent
   public Dimension getPreferredSize ()
   {
       return _size;
   }
   
   
   @Override // from MediaPanel
   protected void paintBehind (Graphics2D gfx, Rectangle dirtyRect)
   {
       super.paintBehind(gfx, dirtyRect);
 
       // fill in our background color
       //new Color(0x6699CC)
       gfx.setColor(new Color(252,236,202));//new Color(238,238,238));
       gfx.fill(dirtyRect);
       //here we'll actually draw all the interfacy widgets
   }
   
   
   
     /**
     * The _size of the Reversi board.
     */
    protected Dimension _size = new Dimension(220, 140);

    /** The territory view (which we need to grab images from) */
    protected CBTerritoryView _tview;
    
    /** Provides access to client services. */
    protected ToyBoxContext _ctx;

    /** A reference to our game object. */
    protected CBObject _gameobj;
    
   /** The game controller to which we dispatch user actions. */
   protected CBController _ctrl;
    
}